
const fs = require('fs')
const got = require('got')
const moment = require('moment')


module.exports = {
    async req(u) {
        let res = null
        , flag = true
        for(let i = 0; ; i ++) {
            flag = true
            res = await got(u).catch(e => {
                flag = false
                console.log(`# ${e.toString()}`)
            })
            if(!flag) {
                this.elog(`# 正在重试: ${u}`)
                await this.sleep(1500)
                continue
            }
            break
        }
        return res || null
    },
    sleep(ms) {
        return new Promise(resolve => setTimeout(resolve, ms))
    },
    elog(txt) {
        console.log(`[${this.now()}] ${txt}`)
    },
    time() {
        return new Date().toLocaleString()
    },
    now() {
        return moment().format('YYYY-MM-DD kk:mm:ss')
    },
    now2() {
        return moment().format('YYYYMMDDkkmmss')
    },
    trim(str) { //删除左右两端的空格
　　     return str.replace(/(^\s*)|(\s*$)/g, "")
　　},
    // 写文件
    wFile(file, txt = '', flag = 'w') {
        fs.writeFileSync(file, txt, {flag: flag})
    },
}